//app.js
App({
    onLaunch: function () {
        wx.showLoading({
            title: '加载中',
            mask: true
        });
        if (!wx.cloud) {
            console.error('请使用 2.2.3 或以上的基础库以使用云能力')
        } else {
            wx.cloud.init({
                // env 参数说明：
                //   env 参数决定接下来小程序发起的云开发调用（wx.cloud.xxx）会默认请求到哪个云环境的资源
                //   此处请填入环境 ID, 环境 ID 可打开云控制台查看
                //   如不填则使用默认环境（第一个创建的环境）
                env: 'cloud1-6gk8d5fdc00bc026',
                traceUser: true,
            })
        }

        this.globalData = {
            timer: null,
            openid: '',
            user: {
                sid: '', // 学号/工号，数据库中的_id实为openid
                name: '',
                phone_number: '',
                email: '',
                role: ''
            },
            nowOrders: [],
            historyOrders: [],
            getUserInfo: async () => {
                const { openid } = (await wx.cloud.callFunction({ name: 'getOpenId' })).result;
                const db = wx.cloud.database();
                const _user = (await db.collection('user').where({
                    _id: openid
                }).limit(1).get()).data[0];
                getApp().globalData.openid = openid;
                const user = this.globalData.user;
                const _admin = (await db.collection('administrator').where({
                    phone_number: _user.phone_number
                }).limit(1).get()).data[0];
                const _repairman = (await db.collection('repairman').where({
                    phone_number: _user.phone_number
                }).limit(1).get()).data[0];
                const _student = (await db.collection('student').where({
                    phone_number: _user.phone_number
                }).limit(1).get()).data[0];
                if (_admin) {
                    user.name = _admin.name;
                    user.role = 'admin';
                } else if (_repairman) {
                    user.name = _repairman.name;
                    user.role = 'repairman';
                    user.sid = _repairman._id;
                } else if (_student) {
                    user.name = _student.name;
                    user.role = 'student';
                    user.sid = _student._id;
                } else user.role = 'other';
                user.phone_number = _user.phone_number;
                user.email = _user.email;
            },
            getOrders: async (str) => {
                if (this.globalData.user.role === 'other') return;
                if (str !== 'now' && str !== 'history') return;
                var finished = false;
                if (str === 'history') finished = true;
                const orders = (await wx.cloud.callFunction({
                    name: 'getOrderRecord',
                    data: {
                        sid: this.globalData.user.sid,
                        role: this.globalData.user.role,
                        finished: finished
                    }
                })).result;
                orders.reverse();
                this.globalData[`${str}Orders`] = orders;
            },
        }
    }
})
